package org.sunrise.game.game.modules;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.Getter;
import lombok.Setter;
import org.sunrise.game.game.human.HumanObject;
import org.sunrise.game.game.human.HumanObjectManger;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@Getter
@Setter
public class BaseModule {
    private final String humanId; //当前模块所属的玩家id
    private Map<String, String> dataMap = new HashMap<>(); //每个模块要存储的数据

    public BaseModule(String humanId) {
        this.humanId = humanId;
    }

    /**
     * 模块初始化
     */
    public void init() {

    }

    /**
     * 加载数据
     */
    public void load() {

    }

    /**
     * 保存数据
     */
    public void save() {
    }

    /**
     * db加载完成，通知客户端的数据
     */
    public void sendToClient() {

    }

    /**
     * 每日刷新
     */
    public void dailyReset() {

    }

    /**
     * 每周刷新
     */
    public void weekReset() {

    }

    public HumanObject getHuman() {
        return HumanObjectManger.getHumanObject(humanId);
    }

    public <T> void getDbData(String key, TypeReference<T> typeReference, Consumer<T> func) {
        String value = dataMap.get(key);
        if (value == null) {
            return;
        }
        func.accept(JSON.parseObject(value, typeReference));
    }

    public void putDbData(String key, Object value) {
        dataMap.put(key, JSON.toJSONString(value));
    }
}
